/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package conceitos.DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 *
 * @author Antonio
 */
public class Conexao {
    // parametros.
    private static final String driver = "org.postgresql.Driver";
    private static final String url = "jdbc:postgresql://localhost:5432/conceitosGame";
    private static final String usuario = "postgres";
    private static final String senha = "katana";
    private static Connection conn = null;
    // abrir conexao com o banco de dados.

    public static Connection getInstance() {
        if (conn == null) {
            // preciso abrir.
            try {
                Class.forName(driver);
                conn = DriverManager.getConnection(url, usuario, senha);
//                JOptionPane.showMessageDialog(null, "Conexão Aberta com Sucesso!");
                conn.setAutoCommit(false);
            } catch (ClassNotFoundException ex) {
                JOptionPane.showMessageDialog(null, "Driver do Banco não Encontrado.");
                System.exit(1);
            } catch (SQLException ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(null, "Erro ao Abrir Conexao!\n" +
                                              "Erro: " + ex.getMessage());
                System.exit(1);
            }
        }
        return conn;
    }

    public static boolean commit() {
        if (conn == null) {
            return false;
        }
        try {
            conn.commit();
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }
    public static boolean rollback() {
        if (conn == null) {
            return false;
        }
        try {
            conn.rollback();
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }
}
















